import { fileURLToPath, URL } from 'node:url'

import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import type { ProxyOptions } from 'vite'

// 定义代理配置类型
const proxyConfig: Record<string, string | ProxyOptions> = {
  // 图片请求代理配置
  '/api/': {
    target: 'http://localhost:8081/', // 目标图片服务器地址
    changeOrigin: true, // 允许跨域
    rewrite: (path) => path.replace(/^\/api/, ''), // 重写路径

  },

}

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
     // ...
     AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  server: {
    open: true, // 自动打开浏览器
    proxy: proxyConfig
  },

  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  },
})
